package com.example.camerademoexcercies1.tool;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static com.example.camerademoexcercies1.tool.BitmapTool.getBitmapDegree;
import static com.example.camerademoexcercies1.tool.BitmapTool.rotateBitmapByDegree;

public
class GetBitmap {
    //    获取对应路径的bitmap
    public static Bitmap getBitmap(String rootPath) {
        File finalFile = new File(rootPath);
        if (finalFile.exists()) {
            final ByteArrayOutputStream stream = new ByteArrayOutputStream();
            FileInputStream inputStream = null;
            try {
                inputStream = new FileInputStream(finalFile);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) != -1) {
                    stream.write(buffer, 0, len);
                }
                inputStream.close();
                int degree = getBitmapDegree(finalFile.getAbsolutePath());
                LogTool.log("加载图片:\t" + finalFile.getAbsolutePath() + "\t大小为：" + stream.size() / 1024 + "kb" + "\t旋转角度：" + degree);
                byte[] imageBytes = stream.toByteArray();
                return rotateBitmapByDegree(BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length),degree);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        LogTool.log("文件：\t" + finalFile.toString() + "\t不存在");
        return null;
    }

}
